<?php
namespace Moblie\Controller;
use Face\Controller\CommonController;

/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/6/30
 * Time: 15:00
 * 移动端
 */
class IndexController extends CommonController
{
    public function index()
    {
        $this->assign('lun',M('slide')->where(['slide_cid'=>1])->select());
        /*头条*/
        $data2 = M('add')->where(['check'=>1])->order('sort desc')->limit('2')->select();
        foreach ($data2 as $k =>$v){
            $data2[$k]['city'] = M('city')->where(['id' => $v['city']])->find();
        }
        /*时间*/
        $data1 = M('add')->where(['check'=>1])->order('id desc')->limit('20')->select();
        foreach ($data1 as $k =>$v){
            $data1[$k]['city'] = M('city')->where(['id' => $v['city']])->find();
        }
        $this->assign(['sort'=>$data2,'id'=>$data1]);
        //(new WechatController())->ticket();
        $this->display();
    }

    /*查找*/
    public function ser()
    {
        $ser = i('post.ser');
        $where= array('like','%'.$ser.'%');
        if(IS_POST){
            if(trim(empty($ser))){
                $this->error('查询内容不能为空');
            }
            $count      = M('add')->where(['title|cityname'=>$where,'check'=>1])->count();
            if($count==0){
                $this->error('没有找到你要找的内容,抱歉了。');
            }
            $Page       = new \Think\Page($count,10);
            $Page->setConfig('theme',"%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
            $show       = $Page->show();
            $list = M('add')->where(['title|cityname'=>$where,'check'=>1])->order('sort desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            foreach ($list as $k=>$v) {
                $list[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
            }

            $this->assign(['page'=>$show,'data'=>$list]);
            $this->display();
        }else{
            $this->display();
        }
    }

    /*分类页*/
    public function type()
    {
        $count      = M('add')->where(['check'=>1])->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme'," %FIRST% %UP_PAGE%  %DOWN_PAGE% %END%");
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('add')->where(['check'=>1])->order('sort desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $k=>$v) {
            $list[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
        }

        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }

    /*内容页*/
    public function content()
    {

        $id = i('get.id');
        $this->code($id);
        $data = M('add')->where(['id'=>$id])->find();
        $data10 = M('add')->where(['id'=>$id])->find();
        $data['city'] = M('city')->where(['id'=>$data['city']])->find();
        $read = $data['read']+1;
        $data1 = [
            'read'=>$read,
        ];
        $where  = ['neq',$id];
        M('add')->where(['id'=>$id])->save($data1);

        $data2 = M('add')->where(['id' =>$where,'city'=>$data10['city'],'check'=>1])->order('id desc')->limit(3)->select();
        if($data2==NULL){
            $data2 = M('add')->where(['id' =>$where,'check'=>1])->order('id desc')->limit(3)->select();
        }
        $this->assign('other',$data2);
        $data3 = M('city')->where(['pid'=>0])->select();
        foreach ($data3 as $k=>$v){
            $data3[$k]['des'] = M('city')->where(['pid' => $v['id']])->select();
        }
        $this->assign('city',$data3);
        $this->assign('ad',$data);

        $this->display();

    }
    /*二位嘛*/
    public function code($aa = '')
    {
        vendor("Phpqrcode.phpqrcode");
        //$data = 'http://www.hahazc.com/index.php?g=Moblie&m=Index&a=index';
        $data = 'http://'.$_SERVER['HTTP_HOST'].'/index.php?g=Moblie&m=Index&a=content&id='.$aa;
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 4;
        //$path = './public/two_code/'.'index'.'.png';
        $path = './public/two_code/'.$aa.'.png';

        \QRcode::png($data, $path, $level, $size) ;
    }
    /*城市分类*/
    public function citytype()
    {
        $id = i('get.id');
        $this->assign('city',M('city')->where(['id'=>$id])->field('name')->find());
        $count      = M('add')->where(['city'=>$id,'check'=>1])->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,7);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme'," %FIRST% %UP_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('add')->where(['city'=>$id,'check'=>1])->order('sort desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $k=>$v) {
            $list[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
        }
        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }
}